//
//  hydateBaseHandle.m
//  豆瓣
//
//  Created by hy on 16/1/8.
//  Copyright © 2016年 hongyu. All rights reserved.
//

#import "hydateBaseHandle.h"
#import "HdModel.h"
#import "HyModel.h"
@implementation hydateBaseHandle


+ (hydateBaseHandle *)shareDataBaseHandle {
    static hydateBaseHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[hydateBaseHandle alloc] init];
    });
    return handle;
}

- (void)openbd {
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sbPath = [document stringByAppendingPathComponent:@"doubai.sqlite"];
    NSLog(@"沙盒路径 : %@", sbPath);
    int result = sqlite3_open(sbPath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }
    else {
        NSLog(@"打开失败");
    }
}
- (void)createtable {
    NSString *sql = [NSString stringWithFormat:@"create table if not exists Activity(number integer primary key autoincrement, title text)"];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建活动表成功");
    }
    else {
        NSLog(@"创建活动表失败");
    }
}
- (void)insertModel:(NSString *)title {
    NSString *sql = [NSString stringWithFormat:@"insert into Activity(title) values('%@')", title];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else {
        NSLog(@"插入失败");
    }
}
- (NSMutableArray *)selectDate {
    NSString *sql = [NSString stringWithFormat:@"select *from Activity"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            HdModel *model = [HdModel hdmodel];
            model.title = [NSString stringWithUTF8String:(const char*)title];
            [array addObject:model];
        }
    }else {
        NSLog(@"查询失败");
    }
    return array;
}
- (void)deleteDate {
    NSString *SQL = [NSString stringWithFormat:@"delete from Activity"];
    int result = sqlite3_exec(db, SQL.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else {
        NSLog(@"删除失败");
    }
    
}
/**创建电影表单*/
- (void)createMovieTable {
    NSString *str = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, title text)"];
    int result = sqlite3_exec(db, str.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建电影表成功");
    }
    else {
        NSLog(@"创建失败");
    }
}
- (void)insertmovie:(NSString *)title {
    NSString *SQL = [NSString stringWithFormat:@"insert into Movie(title) values('%@')", title];
    int result = sqlite3_exec(db, SQL.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入电影成功");
    }
    else {
        NSLog(@"插入电影失败");
    }
}
- (NSMutableArray *)selectMovieDate {
        NSString *sql = [NSString stringWithFormat:@"select *from Movie"];
        sqlite3_stmt *stmt = nil;
        int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
        NSMutableArray *array = [NSMutableArray array];
        if (result == SQLITE_OK) {
            while (sqlite3_step(stmt) == SQLITE_ROW) {
                const unsigned char *title = sqlite3_column_text(stmt, 1);
                HyModel *model = [HyModel model];
                model.title = [NSString stringWithUTF8String:(const char*)title];
                [array addObject:model];
            }
        }else {
            NSLog(@"查询失败");
        }
        return array;
}
@end
